package com.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.entity.XueshengxuankeEntity;
import com.service.XueshengxuankeService;
import com.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/kechengxinxi2")
public class Kechengxinxi2Controller {

    @Autowired
    private XueshengxuankeService xueshengxuankeService;

    /**
     * 根据课程名称查询选课人数
     */
    @GetMapping("/count/{courseName}/{banji}")
    public R getStudentCount(@PathVariable String courseName,
                           @PathVariable String banji) {
        // 构建查询条件
        EntityWrapper<XueshengxuankeEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("kechengmingcheng", courseName);
        wrapper.eq("banji",banji);
        
        // 查询选课人数
        int count = xueshengxuankeService.selectCount(wrapper);
        
        return R.ok().put("count", count);
    }

    /**
     * 获取课程的学生列表
     */
    @GetMapping("/students/{courseName}/{banji}")
    public R getStudentList(@PathVariable String courseName,
                           @PathVariable String banji,
                           @RequestParam Map<String, Object> params) {
        // 获取分页参数
        int page = Integer.parseInt((String)params.get("page"));
        int limit = Integer.parseInt((String)params.get("limit"));

        System.out.println(courseName);
        System.out.println(banji);
        System.out.println(params);

        // 构建查询条件
        EntityWrapper<XueshengxuankeEntity> wrapper = new EntityWrapper<>();
        wrapper.eq("kechengmingcheng", courseName);
        wrapper.eq("banji",banji);

        // 查询学生列表
        Page<XueshengxuankeEntity> pageData = xueshengxuankeService.selectPage(
            new Page<>(page, limit),
            wrapper
        );
        
        return R.ok()
                .put("list", pageData.getRecords())
                .put("total", pageData.getTotal());
    }
}
